﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace 门诊医生工作站
{
    public partial class frm_OnDuty : Form
    {
        public frm_OnDuty(string str)
        {
            InitializeComponent();        
            this.txb_No.Text = str;
            this.Office();
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlCommand sqlCommand1 = new SqlCommand();
            sqlCommand1.Connection = sqlConnection;
            sqlCommand1.CommandText = $"SELECT* FROM dbo.tb_Doctor WHERE No = @No";//查询医生表
            sqlCommand1.Parameters.AddWithValue("No", this.txb_No.Text);
            sqlConnection.Open();
            SqlDataReader sqlDataReader = sqlCommand1.ExecuteReader();//进入值班设置的初始化，直接固定姓名，科室
            if (sqlDataReader.Read())
            {
                this.txb_DoctorName.Text = sqlDataReader["Name"].ToString();
                this.cmb_Office.SelectedValue = (int)sqlDataReader["OfficeNo"];
            }
            sqlConnection.Close();
        }
        //点击打卡签退按钮
        private void btn_Set_Click(object sender, EventArgs e)
        {          
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = $"INSERT dbo.tb_OnDuty(No,OfficeNo,DoctorName,StartTime,EndTime,Date,OnDutyTime)VALUES(@No,@OfficeNo,@DoctorName,@StartTime,@EndTime,@Date,@OnDutyTime)";
            sqlCommand.Parameters.AddWithValue("@No", this.txb_No.Text);
            sqlCommand.Parameters.AddWithValue("@OfficeNo", this.cmb_Office.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@DoctorName", this.txb_DoctorName.Text);
            sqlCommand.Parameters.AddWithValue("@StartTime", this.dtp_StartTime.Value);
            sqlCommand.Parameters.AddWithValue("@EndTime", this.dtp_EndTime.Value);
            sqlCommand.Parameters.AddWithValue("@Date", this.dtp_Date.Value);
            sqlCommand.Parameters.AddWithValue("@OnDutyTime", (this.dtp_EndTime.Value - this.dtp_StartTime.Value).TotalHours);
            sqlConnection.Open();
            int rowAffected = sqlCommand.ExecuteNonQuery();
            if (((this.dtp_EndTime.Value - this.dtp_StartTime.Value).TotalHours)<1)
              {
                MessageBox.Show("未达到值班最低时长，不可签退！");
                SqlCommand sqlCommand2 = new SqlCommand();
                sqlCommand2.Connection = sqlConnection;
                sqlCommand2.CommandText = $"DELETE FROM dbo.tb_OnDuty WHERE OnDutyTime<0 ";//若值班时间为负，则删除该记录；
                int rowAffect2 = sqlCommand2.ExecuteNonQuery();
                //MessageBox.Show($"已删除{rowAffect2}行");
            }
            if (((this.dtp_EndTime.Value - this.dtp_StartTime.Value).TotalHours) >=1&&rowAffected !=0)
              {             
                MessageBox.Show($"签退成功！本次值班时间为{(this.dtp_EndTime.Value-this.dtp_StartTime.Value).TotalHours}小时。");
              }
            sqlConnection.Close();           
        }
       
        //点击查看值班情况按钮
        private void btn_Check_Click(object sender, EventArgs e)
        {
            SqlConnection sqlConnection = new SqlConnection();//声明并实例化SQL连接
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;//借助App.config连接数据库
            SqlCommand sqlCommand = new SqlCommand();
            DataTable dataTable = new DataTable();//保存一整张表
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;//接收查询结果
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = $"SELECT No AS 医生编号,DoctorName AS 医生姓名,Date AS 值班日期,OnDutyTime AS 值班时长 FROM dbo.tb_OnDuty WHERE No=@No AND DoctorName=@DoctorName AND OfficeNo=@OfficeNo";
            sqlCommand.Parameters.AddWithValue("@No", this.txb_No.Text);
            sqlCommand.Parameters.AddWithValue("@OfficeNo", this.cmb_Office.SelectedValue);
            sqlCommand.Parameters.AddWithValue("@DoctorName", this.txb_DoctorName.Text);
            sqlConnection.Open();
            sqlDataAdapter.Fill(dataTable);//将接收到的结果填充到dataTable里面
            this.dgv_OnDuty.DataSource = dataTable;
            //this.cmb_Office.ValueMember = "No";//选中的No对应OfficeNo
            //this.txb_DoctorName.DisplayMember = "Name";//要显示的列名称
            sqlConnection.Close();
        }
        private void Office()
        {
            SqlConnection sqlConnection = new SqlConnection();//声明并实例化SQL连接
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;//借助App.config连接数据库
            SqlCommand sqlCommand = new SqlCommand();
            DataTable dataTable = new DataTable();//保存一整张表
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            sqlDataAdapter.SelectCommand = sqlCommand;//接收查询结果
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandText = $"SELECT * FROM dbo.tb_Office";
            sqlConnection.Open();
            sqlDataAdapter.Fill(dataTable);//将接收到的结果填充到dataTable里面
            this.cmb_Office.DataSource = dataTable;
            this.cmb_Office.ValueMember = "No";//选中的No对应OfficeNo
            this.cmb_Office.DisplayMember = "Name";//要显示的列名称
            sqlConnection.Close();
        }
        private void cmb_Office_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
